package com.origin.oss.configuration;

import cn.hutool.core.text.CharSequenceUtil;
import com.github.tobato.fastdfs.FdfsClientConfig;
import com.origin.oss.operation.download.product.FastDFSDownloader;
import com.origin.oss.factory.FileUploaderFactory;
import com.origin.oss.operation.copy.product.FastDFSCopier;
import com.origin.oss.operation.delete.product.FastDFSDeleter;
import com.origin.oss.operation.preview.product.FastDFSPreviewer;
import com.origin.oss.operation.read.product.FastDFSReader;
import com.origin.oss.operation.upload.product.AliyunOSSUploader;
import com.origin.oss.operation.write.product.FastDFSWriter;
import com.origin.oss.util.RedisUtil;
import com.origin.oss.util.FileStorageUtils;
import com.origin.oss.util.concurrent.locks.RedisLock;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;
import org.springframework.jmx.support.RegistrationPolicy;

/**
 * @author origin
 */
@Configuration
@EnableConfigurationProperties({FileStorageProperties.class})
@ComponentScan("com.origin.oss")
@MapperScan("com.origin.oss")
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
@Slf4j
public class FileStorageAutoConfiguration implements InitializingBean {

    @Autowired
    private FileStorageProperties fileStorageProperties;

    @Bean
    public FileUploaderFactory ufopFactory() {
        FileStorageUtils.LOCAL_STORAGE_PATH = fileStorageProperties.getLocalStoragePath();
        String bucketName = fileStorageProperties.getBucketName();
        FileStorageUtils.ROOT_PATH = CharSequenceUtil.isNotBlank(bucketName) ? fileStorageProperties.getBucketName() : "upload";
        return new FileUploaderFactory(fileStorageProperties);
    }

    @Bean
    public FastDFSCopier fastDFSCreater() {
        return new FastDFSCopier();
    }

//    @Bean
//    public FastDFSUploader fastDFSUploader() {
//        return new FastDFSUploader();
//    }

    @Bean
    public FastDFSDownloader fastDFSDownloader() {
        return new FastDFSDownloader();
    }

    @Bean
    public FastDFSDeleter fastDFSDeleter() {
        return new FastDFSDeleter();
    }

    @Bean
    public FastDFSReader fastDFSReader() {
        return new FastDFSReader();
    }

    @Bean
    public FastDFSWriter fastDFSWriter() {
        return new FastDFSWriter();
    }

    @Bean
    public FastDFSPreviewer fastDFSPreviewer() {
        return new FastDFSPreviewer(fileStorageProperties.getThumbImage());
    }

    @Bean
    public AliyunOSSUploader aliyunOSSUploader() {
        return new AliyunOSSUploader(fileStorageProperties.getAliyun());
    }

//    @Bean
//    public MinioUploader minioUploader() {
//        return new MinioUploader(ufopProperties.getMinio());
//    }

//    @Bean
//    public QiniuyunKodoUploader qiniuyunKodoUploader() {
//        return new QiniuyunKodoUploader(ufopProperties.getQiniuyun());
//    }

    @Bean
    public RedisLock redisLock() {
        return new RedisLock();
    }

    @Bean
    public RedisUtil redisUtil() {
        return new RedisUtil();
    }

    @Override
    public void afterPropertiesSet() {
        log.info("FileStorageAutoConfiguration loading...");
    }

}
